from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time


options = Options()
options.add_argument('start-maximized')
options.add_argument('''--disable-gpu''')
options.add_argument('''--no-sandbox''')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(options=options)

# 实在是没有想到，这也是个大坑，各种花里胡哨的打开页面方法，全都不能用，这么写就对了
driver.execute_script("window.open()")

driver.switch_to.window(driver.window_handles[1])

driver.get("chrome://settings/clearBrowserData")

time.sleep(3)

# 点击接触cookie的勾选，这样清除缓存后就不需要重新登陆可以继续爬取任务
cookieButton = driver.execute_script(
    "return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog #tabs #cookiesCheckboxBasic').shadowRoot.querySelector('#checkbox')")
cookieButton.click()
time.sleep(3)
# 清除缓存按钮
clearButton = driver.execute_script(
    "return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')")
clearButton.click()
time.sleep(3)

driver.switch_to.window(driver.window_handles[0])

time.sleep(60*60)